package zyc.web;

import zyc.pojo.Student;
import zyc.pojo.Teacher;
import zyc.service.TeacherService;
import zyc.service.imple.TeacherServiceimple;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Properties;

/**
 * @description:
 * @author: Code-zyc
 * @date: Created in 2020/12/24 8:29
 * @version: v1.0
 * @modified By:
 */
@WebServlet("/teacherServlet")
public class TeacherServlet extends BaseServlet {

    private TeacherService teacherService=new TeacherServiceimple();

    /**
     * @descript :登录功能
     * @author :ZhaoYicong
     * @date :2020/12/24 8:26
     * @Param: null
     * @return :
     * @throws :
     * @since :
     */


    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        int tid = Integer.parseInt(req.getParameter("id"));
        String password = req.getParameter("password");
        Teacher teacher=teacherService.getTeacherBytidPass(tid,password);
        if(teacher!=null){
            req.getRequestDispatcher("studentServlet?action=getstudents").forward(req,resp);
        }else{
            req.setAttribute("alert","用户名或者密码错误");
            System.out.println("静茹 特爱cher");
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }


    }

    public void sendmessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String message = req.getParameter("message");
        Student student = (Student)req.getSession().getAttribute("student");
        System.out.println("进来了");
        String email = student.getEmail();
        try {
            email(message,email,student);
            req.setAttribute("alert","发送成功！");
            System.out.println("到了这一步");
            req.getRequestDispatcher("studentServlet?action=getstudentTostudent&id="+student.getSid()).forward(req,resp);
        } catch (Exception e) {
            req.setAttribute("alert","发送失败！");
            req.getRequestDispatcher("studentServlet?action=getstudentTostudent&id="+student.getSid()).forward(req,resp);


        }


    }


    //发送邮件的方法
    public static void email(String mess, String email,Student student) throws Exception {
        Properties properties = new Properties();

        properties.put("mail.transport.protocol", "smtp");//连接协议

        properties.put("mail.smtp.host", "smtp.qq.com");//主机名

        properties.put("mail.smtp.port", 465);//端口号

        properties.put("mail.smtp.auth", "true");

        properties.put("mail.smtp.ssl.enable", "true");//设置是否使用ssl安全连接--使用

        properties.put("mail.debug", "true");//设置是否显示debug信息true会在控制台显示相关信息
        //得到回话对象
        Session session = Session.getInstance(properties);
        //获取邮件对象
        Message message = new MimeMessage(session);
        //设置发件人邮箱地址
        message.setFrom(new InternetAddress("1049566924@qq.com"));
        //设置收件人邮箱地址
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
        //设置邮件标题
        message.setSubject("学号：" +student.getSid()+"姓名："+student.getName()+"给你发信息了");
        //设置邮件内容
        message.setText( "内容："+mess);
        //得到邮差对象
        Transport transport = session.getTransport();
        //连接自己的邮箱账户
        transport.connect("1049566924@qq.com", "zxuvxjfybjoabdfi");//password为QQ邮箱开通smtp服务后得到的授权码

        //发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
}
